using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class PackageCell : BasePanel, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler
{
    private Transform UIIcon;
    private Transform UIHead;
    private Transform UINew;
    private Transform UISelect;
    private Transform UILevel;
    private Transform UIStars;
    private Transform UIDeleteSelect;

    private PackageLocalItem packageLocalItem;
    private PackageTableItem packageTableItem;

    private Transform SelectAni;
    private Transform MouseOverAni;
    private PackagePanel uiParent;
    public GameObject starPrefab;
    private int starAll = 5;

    private void Awake()
    {
        InitUIName();
    }

    private void InitUIName()
    {
        UIIcon = transform.Find("Top/Icon");
        UIHead = transform.Find("Top/Head");
        UINew = transform.Find("Top/New");
        UILevel = transform.Find("Bottom/LevelText");
        UIStars = transform.Find("Bottom/Stars");
        UISelect = transform.Find("Select");
        UIDeleteSelect = transform.Find("DeleteSelect");
        SelectAni = transform.Find("SelectAni");
        MouseOverAni = transform.Find("MouseOverAni");
        UIDeleteSelect.gameObject.SetActive(false);

        SelectAni.gameObject.SetActive(false);
        MouseOverAni.gameObject.SetActive(false);
    }

    /// <summary>
    /// 根据数据刷新ui
    /// </summary>
    /// <param name="packageLocalItem"></param>
    public void RefreshUI(PackageLocalItem packageLocalItem, PackagePanel uiParent)
    {
        this.packageLocalItem = packageLocalItem;
        this.uiParent = uiParent;
        packageTableItem = GameManager.Instance.GetPackageTableItemByid(packageLocalItem.id);
        //等级
        UILevel.GetComponent<Text>().text = "Lv." + packageLocalItem.level;
        //新获得
        UINew.gameObject.SetActive(packageLocalItem.isNew);
        //星级
        for (int i = 0; i < starAll - packageTableItem.star; i++)
        {
            UIStars.GetChild(i).gameObject.SetActive(false);
        }

        //图标
        Image image = UIIcon.GetComponent<Image>();
        image.sprite = Resources.Load<Sprite>(packageTableItem.imagePath);
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        if (this.uiParent.keyMode == PackageMode.DELETE)
        {
            this.uiParent.AddCheckUId(packageLocalItem.uid);
        }
        if (uiParent.SelectUId.Equals(packageLocalItem.uid))
        {
            return;
        }

        uiParent.SelectUId = packageLocalItem.uid;
        SelectAni.gameObject.SetActive(true);
        SelectAni.GetComponent<Animator>().SetTrigger("in");
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        MouseOverAni.gameObject.SetActive(true);
        MouseOverAni.GetComponent<Animator>().SetTrigger("in");
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        MouseOverAni.gameObject.SetActive(true);
        MouseOverAni.GetComponent<Animator>().SetTrigger("out");
    }

    public void RefreshDeleteState()
    {
        if (uiParent.uids.Contains(packageLocalItem.uid))
        {
            UIDeleteSelect.gameObject.SetActive(true);
        }
        else
        {
            UIDeleteSelect.gameObject.SetActive(false);
        }
    }
}